package com.ff.xyh.service.post.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ff.xyh.common.entity.result.ResultMessage;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.entity.post.PostImg;
import com.ff.xyh.service.post.service.PostImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 帖子照片表 前端控制器
 * </p>
 *
 * @author ff
 * @since 2022-10-26
 */
@RestController
@RequestMapping("/post-service")
public class PostImgController {


    @Autowired
    private PostImgService postImgService;

//    @GetMapping("get-post-img-all")
//    public ResultObject getAllPostImg() {
//        List<PostImg> list = postImgService.list(null);
//        if (list == null || list.isEmpty()){
//            return ResultObject.error().message(ResultMessage.NO_RESULT);
//        } else {
//            Map map = new HashMap();
//            map.put("items", list);
//            return ResultObject.ok().data(map);
//        }
//    }

    @PostMapping("get-post-imgs-id-type")
    public ResultObject getPostImgsByIdAndType(
            @RequestParam(name = "id") String id,
            @RequestParam(name = "type") int type) {
        List<PostImg> items = postImgService.list(new QueryWrapper<PostImg>()
                .eq("post_id", id).eq("post_type", type));
        if (items == null || items.isEmpty()){
            return ResultObject.exposed_error().message(ResultMessage.NO_SUCH_OBJECT);
        } else {
            Map map = new HashMap();
            map.put("items", items);
            return ResultObject.ok().data(map);
        }
    }

    @PostMapping("delete-post-imgs-post-id-type")
    public ResultObject deletePostImgsByPostIdAndType(
            @RequestParam(name = "postId") String postId,
            @RequestParam(name = "type") int type) {
        QueryWrapper<PostImg> wrapper = new QueryWrapper<>();
        wrapper.eq("post_id", postId).eq("post_type", type);

        PostImg postImg = postImgService.getOne(wrapper);
        if (postImg == null){
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        } else {
            if (postImgService.remove(wrapper)) {
                return ResultObject.ok().message("该帖子照片集已删除");
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_DELETE);
            }
        }
    }

    @PostMapping("insert-post-img")
    public ResultObject insertPostImg (
            @RequestBody PostImg postImg) {
        QueryWrapper<PostImg> wrapper = new QueryWrapper<>();
        wrapper.eq("post_id", postImg.getPostId())
                .eq("img_url", postImg.getImgUrl())
                .eq("post_type", postImg.getPostType());
        if (postImgService.getOne(wrapper) != null){
            return ResultObject.error().message(ResultMessage.OBJECT_ALREADY_EXISTS);
        } else {
            if (postImgService.save(postImg)) {
                Map map = new HashMap();
                map.put("data", postImg);
                return ResultObject.ok().data(map);
            } else {
                return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT_BUT_FAILED_TO_INSERT);
            }
        }
    }

    @PostMapping("insert-post-imgs")
    public ResultObject insertPostImgs (
            @RequestBody List<PostImg> postImgs) {
        if (postImgService.saveBatch(postImgs))
            return ResultObject.ok().data("items", postImgs);
        else
            return ResultObject.error().message(ResultMessage.FAILED_TO_INSERT);
    }

    @PostMapping("update-post-img")
    public ResultObject updatePostImg(
            @RequestBody PostImg postImg) {
        QueryWrapper<PostImg> wrapper = new QueryWrapper<>();
        wrapper.eq("post_id", postImg.getPostId())
                .eq("post_type", postImg.getPostType());
        if (postImgService.getOne(wrapper) == null){
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        } else {
            if (postImgService.update(postImg,wrapper)) {
                Map map = new HashMap();
                map.put("data", postImg);
                return ResultObject.ok().data(map);
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_UPDATE);
            }
        }
    }
}

